﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class en_news_list : System.Web.UI.Page
{
    PagedDataSource PDS = new PagedDataSource();
    private string ConnStr = ConfigurationManager.AppSettings["ConnectionString"];
    private string strsql = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            this.bind();
        }
    }

    private void bind()
    {
        int TotalCount = 0;//总记录数
        int TotalPage = 1; //总页数

        SqlConnection conn = new SqlConnection(ConnStr);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [Title],[Optime] FROM [En_NewsTable] order by id desc", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "En_NewsTable");
        DataView dv = ds.Tables[0].DefaultView;

        TotalCount = dv.Count;
        PDS.DataSource = dv;
        conn.Close();
        PDS.AllowPaging = true;
        PDS.PageSize = 20;
        int CurPage;
        if (Request.QueryString["Page"] != null)
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        else
            CurPage = 1;

        if (TotalCount == 0)
            TotalPage = 1;
        else
        {
            if (TotalCount % PDS.PageSize == 0)
                TotalPage = TotalCount / PDS.PageSize;
            else
                TotalPage = TotalCount / PDS.PageSize + 1;
        }

        PDS.CurrentPageIndex = CurPage - 1;
        lblCurrentPage.Text = "Total " + TotalCount.ToString() + " Records   Current：" + CurPage.ToString() + "/" + TotalPage;

        lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
        if (!PDS.IsFirstPage)
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

        if (!PDS.IsLastPage)
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
        lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TotalPage;

        Repeater1.DataSource = PDS;
        Repeater1.DataBind();
    }
}
